#!/bin/bash

menu(){
    echo "1、创建用户"
    echo "2、删除指定用户"
    echo "3、修改指定用户（用户id，用户属组，用户家目录）"
}
menu2(){
    echo "1、修改用户id"
    echo "2、修改用户属组"
    echo "3、修改用户家目录"

}

add(){
    read -p "输入用户名:" username
    id $username &>/dev/null &&echo "用户已存在" || useradd $username &>/dev/null && echo "创建成功" || echo "创建失败"
}

del(){
    read -p "输入用户名:" username
    id $username &>/dev/null || echo "用户不存在" && userdel -r $username &>/dev/null && echo "删除成功" || echo "删除失败"
}

modify(){
    read -p "输入用户名:" username
    if id $username &>/dev/null
    then
	menu2
	read -p "请输入:" choice2
	case $choice2 in
	1)
	    read -p "输入uid:" uid
	    usermod -u $uid $username &>/dev/null && echo "修改成功" || echo "修改失败"
	;;
	2)
	    read -p "输入gid:" gid
	    usermod -g $gid &>/dev/null && echo "修改成功" || echo "修改失败"
	;;
	3)
	    read -p "输入家目录:" home
	    usermod -d $home $username &>/dev/null && echo "修改成功" || echo "修改失败"
	;;
	*)
	echo "输入不合法"
	esac
    else
        echo "用户不存在"
    fi
}

while true
do
    menu
    read -p "请输入(按q退出)：" choice
    case $choice in
    1)
        add
        ;;
    2)
        del
        ;;
    3)
        modify
        ;;
    q)
	break
	;;
    *)
        echo "输入不合法"
    esac
done

